"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 硬件故障类--网卡
Case Name   : 内存不足，启动数据库
Create At   : 2020-12-29
Owner       : n@ningyali
Description :
    1、创建全局临时表，并对临时表DML操作
    2、模拟网卡闪断：断网3s，间隔3s，重复10次
Expect      :
    1、创建全局临时表成功，dml操作成功
    2、网卡闪断故障注入成功
    3、数据库集群不感知，网卡闪断期间，数据库操作稍有延迟，但dml操作正常
History     :
    2021/1/20 @wan005:适配最新CFE故障打印
"""

import unittest

from yat.test import Node

from testcase.utils.ComThread import *
from testcase.utils.Common import *
from testcase.utils.CommonSH import *


class DMLTempTableAndNetLinkFlash(unittest.TestCase):
    primary_sh = CommonSH('PrimaryDbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('-----------Opengauss_Reliability_Hardware_Case005:开始执行------------')
        self.primary_root_node = Node(node='PrimaryRoot')
        self.primary_user_node = Node(node='PrimaryDbUser')
        self.com = Common()
        self.Constant = Constant()
        self.t_name = 't_reliability_hardware005'

    def test_main(self):
        text = '--step1:创建全局临时表，并对临时表DML操作 ' \
               'expect:创建全局临时表成功，dml操作成功--'
        self.log.info(text)
        sql_cmd = f"drop table if exists {self.t_name}; create global temp table {self.t_name}(i int, info varchar(50));"
        self.log.info(sql_cmd)
        msg = self.primary_sh.executDbSql(sql_cmd)
        self.log.info(msg)
        self.assertIn(self.Constant.CREATE_TABLE_SUCCESS, msg)

        self.log.info("-------------对临时表DML操作---------------")
        sql_cmd = f"insert into {self.t_name} values(1, '网卡闪断时插入数据'); select count(*) from {self.t_name};"
        self.log.info(sql_cmd)
        sql_thread = ComThread(self.primary_sh.cycle_exec_sql, args=(sql_cmd, 1000))
        sql_thread.setDaemon(True)
        sql_thread.start()

        self.log.info("-------------获取网卡---------------")
        shell_cmd = "ifconfig | head -1"
        self.log.info(shell_cmd)
        msg = self.primary_root_node.sh(shell_cmd).result()
        dev_name = msg.split(":")[0].strip()
        self.log.info(msg)

        text = '--step2:模拟网卡闪断：断网3s，间隔3s，重复10次 ' \
               'expect:网卡闪断故障注入成功--'
        self.log.info(text)
        inject_command = f"rNetLink_flash (repeat,duration,interval,dev) values (3,2,10,{dev_name})"
        msg = self.com.cfe_inject(self.primary_root_node, inject_command)
        self.log.info(msg)
        self.assertIn(self.Constant.cfe_inject_netflash_success_msg, msg,
                      '执行失败:' + text)

        self.log.info("-------------校验对临时表DML操作结果---------------")
        sql_thread.join(60 * 10)
        sql_result = sql_thread.get_result()
        self.assertTrue(sql_result)

    def tearDown(self):
        sql_cmd = f"drop table if exists {self.t_name};"
        self.log.info(sql_cmd)
        msg = self.primary_sh.executDbSql(sql_cmd)
        self.log.info(msg)
        self.log.info('--------Opengauss_Reliability_Hardware_Case005:执行完成-----------')
